//
//  StatusTransmitView.swift
//  Weibo
//
//  Created by 潇 on 15/12/26.
//  Copyright © 2015年 潇. All rights reserved.
//

import UIKit
import SnapKit
//准发微博  转发微博正文 + 配图视图
class StatusTransmitView: UIView {

    //定义约束属性 保存当前视图的底部约束
    var bottomConstraints: Constraint?
    
    //MARK: 定义外部模型属性
    var retweetedStatus: Status? {
        didSet {
            transmitLabel.text = "@" + (retweetedStatus?.user?.name ?? "") + (retweetedStatus?.text ?? "")
            pictureView.imageURLs = retweetedStatus?.imageURLs
            
            //卸载掉底部约束
            self.bottomConstraints?.uninstall()
            
            //根据是否有配图视图 重新添加约束
            if let urls = retweetedStatus?.imageURLs where urls.count > 0 {
                //有配图视图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
                
            }else {
                //没有配图视图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(transmitLabel.snp_bottom).offset(StatusCellMarigin).constraint
                })
            }
        }
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        //设置视图
        setupUI()
        backgroundColor = UIColor(white: 0.95, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置页面和布局
    private func setupUI() {
        addSubview(transmitLabel)
        addSubview(pictureView)
        
        //约束
        transmitLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(transmitLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(transmitLabel.snp_left)
            
        }
        
        //使转发微博自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            //1.添加约束
            //2.记录底部约束
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
            
        }
    }
    
    //MARK: 懒加载所有的子视图
    private lazy var transmitLabel: UILabel = UILabel(title: "转发微博", color:  UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMarigin)
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    

}
